package pl.edu.mimuw.irs.rcp.controller.editor;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.gef.DefaultEditDomain;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.editparts.FreeformGraphicalRootEditPart;
import org.eclipse.gef.editparts.ScalableRootEditPart;
import org.eclipse.gef.tools.SelectionTool;
import org.eclipse.gef.ui.parts.GraphicalEditor;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;

import pl.edu.mimuw.irs.core.model.analysis.AnalysisWorkflow;
import pl.edu.mimuw.irs.rcp.controller.factory.EditPartFactory;

public class AnalysisWorkflowEditor extends GraphicalEditor implements WorkflowEditor {

	// *** Static attributes
	public static final String ID = "pl.edu.mimuw.irs.rcp.controller.analysisworkfloweditor";

	// *** Attributes
	private AnalysisWorkflow model;
	
	public AnalysisWorkflow getModel() {
		return model;
	}// TODO to remove?
	
	// *** Methods

	public AnalysisWorkflowEditor() {
		setEditDomain(new DefaultEditDomain(this));
		getEditDomain().setActiveTool(new SelectionTool());
	}
		
	@Override
	protected void createGraphicalViewer(Composite parent) {
		super.createGraphicalViewer(parent);

		// Copied from GraphicalView.createPartControl()
		
		model = ((AnalysisWorkflowEditorInput)getEditorInput()).getAnalysisWorkflow();
		
		//getGraphicalViewer().createControl(parent);
		getGraphicalViewer().setRootEditPart(new ScalableRootEditPart());
		getGraphicalViewer().setEditPartFactory(new EditPartFactory());
		getGraphicalViewer().setContents(model);
		getGraphicalViewer().getControl().setBackground(new Color(null, 255,255,255));

		//WorkflowEditor editor = new WorkflowEditor();
		getGraphicalViewer().setEditDomain(new DefaultEditDomain(this));
		
		//ContextMenuProvider contextMenuProvider = new WorkflowContextMenuProvider(getGraphicalViewer(), this);
		//getGraphicalViewer().setContextMenu(contextMenuProvider);
		
		// Initialize the viewer, 'parent' is the
		// enclosing IRS windowframe
		//getGraphicalViewer().createControl(parent);
		getGraphicalViewer().getControl().setBackground(new Color(null, 255,255,255));
	}
	
	@Override
	protected void initializeGraphicalViewer() {
		//model = new IRSWorkflow();
		//model.createSampleModel();

		getGraphicalViewer().setContents(model);
	}

	/*
	 * @see org.eclipse.gef.ui.parts.GraphicalEditor#configureGraphicalViewer()
	 */
	@Override
	protected void configureGraphicalViewer() {
		super.configureGraphicalViewer();
		GraphicalViewer viewer = getGraphicalViewer();

		viewer.setRootEditPart(new FreeformGraphicalRootEditPart());
		viewer.setEditPartFactory(new EditPartFactory());
		
	}
	
	@Override
	protected void setInput(IEditorInput input) {
		super.setInput(input);
	}
	
	@Override
	public void doSave(IProgressMonitor monitor) {
		// TODO Auto-generated method stub
		
	}

}
